package sapling.api;

import io.swagger.annotations.ApiOperation;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sapling.api.model.*;
import sapling.dao.AreaLocationDao;
import sapling.dao.LandMassDao;
import sapling.dao.TreeDao;
import sapling.model.AreaLocation;
import sapling.model.LandMass;
import sapling.model.Tree;
import sapling.model.TreeWell;
import sapling.service.*;
import sapling.utils.FileUtil;
import sapling.utils.LetterUtil;
import sapling.utils.Msg;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by Administrator on 2017/6/16.
 */
@RestController
@RequestMapping("/api/landMass")
public class LandMassApi {
    @Autowired
    private TreeWellService treeWellService;
    @Autowired
    private LandMassService landMassService;
    @Autowired
    private AreaLocationDao areaLocationDao;
    @Autowired
    private AreaLocationService areaLocationService;
    @Autowired
    private LandMassDao landMassDao;
    @Autowired
    private TreeDao treeDao;
    @Autowired
    private TreeService treeService;
    @Autowired
    private PushService pushService;
    @Autowired
    private  StorageService storageService;

    /*@GetMapping("/landall")
    public Msg<String> randomArea(@RequestParam String areaname){
        AreaLocation area = areaLocationDao.findbyname(areaname);
        List<LandMass> landMasses = area.getLandMasses();
        for(LandMass landmass:landMasses){
            double random = Math.random();
            if(random<0.6) {
                if(landmass.getGenerate()==0) {
                    randomToLand(areaname, landmass.getSerialNumber());
                }
            }
        }
        return Msg.newMsg("success");
    }

    public String randomToLand(@RequestParam String areaname,@RequestParam String serialnumber) {
        AreaLocation area = areaLocationDao.findbyname(areaname);
        LandMass landmass = landMassDao.findSerialNumber(area.getAreaLocation_id(), serialnumber);
        Msg<ApiAreaSummary> apiAreaSummaryMsg = this.treeSpecies2(1, 4, landmass.getLandMass_id());
        List<ApiTreeSummary> treeSummaryList = apiAreaSummaryMsg.getRes().getTreeSummaryList();
        StringBuilder sb=new StringBuilder();
        for(ApiTreeSummary apiTreeSummary:treeSummaryList){
            double random = Math.random();
            if(random<0.6) {
                sb.append(apiTreeSummary.getTreeWell().getTreeWell_id() + ";");
            }
        }
        String well_ids = sb.toString().substring(0, sb.length() - 1);
        randomadd(well_ids,(int)(Math.random()*10),(int)(Math.random()*10),(int)(Math.random()*10),false,sb.length(),"18910940347",1,"cc",null);
        return "success";
    }
    public Msg<String> randomadd(@RequestParam String treeWell_ids, @RequestParam double chestBreadth, @RequestParam double crownWidth,
                                @RequestParam double height, @RequestParam Boolean clump, @RequestParam int num, @RequestParam String username, @RequestParam int salenumber,@RequestParam(value = "remark", required = false) String remark,@RequestParam(value = "file", required = false) MultipartFile file) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        String imgurl= FileUtil.savefile(file);
        StringBuilder treeids=new StringBuilder();
        for (String value : treeWell_ids.split(";")) {
            Long id = Long.parseLong(value);
            String[] trees={"杨树", "桃树", "柳树"};
            int random = (int)(Math.random()*3);
            Tree tree = treeWellService.saveTreeWell(id, trees[random], chestBreadth, crownWidth, height, clump, num, username, salenumber, serid,remark, imgurl);
            treeids.append(tree.getTree_id()+";");
        }
        pushService.inform("finance","有"+num+"棵苗木需要入库审核");
        return Msg.newMsg(treeids.toString());
    }*/

    @ApiOperation(value = "所有已种植地块", notes = "查询所有已种植地块")
    @GetMapping("/plant")
    public Msg<List<ApiLandMassModel>> list() {
        return Msg.newMsg(landMassService.listLandMass(1));
    }

    @ApiOperation(value = "所有分配树坑未种树地块", notes = "所有分配树坑未种树地块")
    @GetMapping("/leisure")
    public Msg<List<ApiLandMassModel>> leisure() {
        return Msg.newMsg(landMassService.listLandMass(0));
    }

    @ApiOperation(value = "所有未分配树坑地块", notes = "所有未分配树坑地块")
    @GetMapping("/listemptyLandMass")
    public Msg<List<ApiLandMassModel>> listemptyLandMass() {
        return Msg.newMsg(landMassService.listisEmptyLandMass(0));
    }

    @ApiOperation(value = "所有已分配树坑地块", notes = "所有未分配树坑地块")
    @GetMapping("/listnoemptyLandMass")
    public Msg<List<ApiLandMassModel>> listnoemptyLandMass() {
        return Msg.newMsg(landMassService.listisEmptyLandMass(1));
    }

    @ApiOperation(value = "查询地块树木和树坑信息", notes = "查询地块树木和树坑信息")
    @GetMapping("/landmassSummary")
    public Msg<ApiAreaSummary> landmassSummary(@RequestParam long landmassid) {
        ApiAreaSummary apiAreaSummary = landMassService.getlandmassSummary(landmassid);
        return Msg.newMsg(apiAreaSummary);
    }
    @ApiOperation(value = "所有地块分类返回", notes = "所有地块分类返回")
    @GetMapping("/alllandlist")
    public Msg<ApiLandList> alllandlist() {
        ApiLandList res=new ApiLandList();
        res.setEmptylist(landMassService.listisEmptyLandMass(0));
        List<ApiLandMassModel> noemptylist = landMassService.listisEmptyLandMass(1);
        for(ApiLandMassModel apiLandMassModel:noemptylist){
            int treeWell_occupy = apiLandMassModel.getTreeWell_occupy();
            int treeWell_quantity = apiLandMassModel.getTreeWell_quantity();
            if(treeWell_occupy==0){
                res.getTreeareaemptylist().add(apiLandMassModel);
            }else if(treeWell_occupy<treeWell_quantity){
                res.getNofulllist().add(apiLandMassModel);
            }else{
                res.getFulllist().add(apiLandMassModel);
            }
        }
        return Msg.newMsg(res);
    }


    @ApiOperation(value = "单选单个树坑种植", notes = "传入树坑的ID-treeWell_id,树木品种treeSpecies,胸径地径chestBreadth(double型),冠幅crownWidth（double）,高度height（double），是否丛生clump,产地location,price单件,num数量")
    @RequestMapping(value = "/clickPlant", method = RequestMethod.POST)
    public Msg<Tree> clickPlant(@RequestParam Long treeWell_id, @RequestParam String treeSpecies, @RequestParam double chestBreadth,@RequestParam double chestBreadthmax,
                                @RequestParam double crownWidth, @RequestParam double crownWidthmax,
                                  @RequestParam double height, @RequestParam double heightmax,
                                @RequestParam Boolean clump, @RequestParam String username,
                                @RequestParam int salenumber,@RequestParam int salenumbermax,
                                @RequestParam(value = "remark", required = false) String remark,@RequestParam(value = "file", required = false) MultipartFile file
                                ,@RequestParam(value = "file", required = false) double cost) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        String imgurl= FileUtil.savefile(file);
        Tree tree = treeWellService.saveTreeWell(treeWell_id, treeSpecies, chestBreadth, chestBreadthmax,crownWidth,crownWidth, height,heightmax,
                (salenumber>0)||(salenumbermax>0), 1, username, salenumber,salenumbermax,remark, serid, imgurl,cost);
        pushService.inform("finance","有1棵苗木需要出库审核");
        return Msg.newMsg(tree);
    }
    @ApiOperation(value = "多选多个树坑种植", notes = "传入多个树坑的ID-List<treeWell_id>以;分隔,树木品种treeSpecies,胸径地径chestBreadth(double型),冠幅crownWidth（double）,高度height（double），是否丛生clump,num数量, salenumber枝条数")
    @PostMapping("/multiplePlant")
    @Transactional
    public Msg<List<Tree>> multiplePlant(@RequestParam String treeWell_ids, @RequestParam String treeSpecies, @RequestParam double chestBreadth,
                                         @RequestParam double crownWidth,
                                         @RequestParam double height,
                                         @RequestParam Boolean clump,
                                         @RequestParam int num, @RequestParam String username,
                                         @RequestParam(required = false) int salenumber,
                                         @RequestParam(value = "remark", required = false) String remark, @RequestParam(value = "file", required = false) MultipartFile file
            ) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        List<Tree> trees=new ArrayList<>();
        String imgurl= Objects.nonNull(FileUtil.savefile(file))?FileUtil.savefile(file):null;
        int number=0;
        for (String value : treeWell_ids.split(";")) {
            Long id = Long.parseLong(value);
            Tree tree= treeWellService.saveTreeWell(id, treeSpecies, chestBreadth,-1,crownWidth,-1, height,height,
                    clump,
                    num, username,
                    salenumber,-1,
                    remark, serid,imgurl,-1);
            if(Objects.nonNull(tree)){
                trees.add(tree);
                number++;
            }
        }
        if(number>0){
            pushService.inform("finance","有"+number+"棵苗木需要出库审核");
        }
        return Msg.newMsg(trees);
    }
    @ApiOperation(value = "多选多个树坑种植", notes = "传入多个树坑的ID-List<treeWell_id>以;分隔,树木品种treeSpecies,胸径地径chestBreadth(double型),冠幅crownWidth（double）,高度height（double），是否丛生clump,num数量, salenumber枝条数")
    @PostMapping("/multiplePlant2")
    @Transactional
    public Msg<List<Tree>> multiplePlant2(@RequestParam String treeWell_ids, @RequestParam String treeSpecies, @RequestParam double chestBreadth, @RequestParam(required = false,defaultValue = "-1") double chestBreadthmax,
                                         @RequestParam double crownWidth,@RequestParam(required = false,defaultValue = "-1") double crownWidthmax,
                                         @RequestParam double height, @RequestParam(required = false,defaultValue = "-1") double heightmax,
                                         @RequestParam Boolean clump,
                                         @RequestParam int num, @RequestParam String username,
                                         @RequestParam(required = false) int salenumber,
                                         @RequestParam(required = false) int salenumbermax,
                                         @RequestParam(value = "remark", required = false) String remark, @RequestParam(value = "file", required = false) MultipartFile file
                                        ,@RequestParam(required = false) double cost) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        List<Tree> trees=new ArrayList<>();
        String imgurl= Objects.nonNull(FileUtil.savefile(file))?FileUtil.savefile(file):null;
        int number=0;
        for (String value : treeWell_ids.split(";")) {
            Long id = Long.parseLong(value);
            Tree tree= treeWellService.saveTreeWell(id, treeSpecies, chestBreadth,chestBreadthmax,crownWidth,crownWidthmax, height,height,
                    (salenumber>0)||(salenumbermax>0),
                    num, username,
                    salenumber,salenumbermax,
                    remark, serid,imgurl,cost);
            if(Objects.nonNull(tree)){
                trees.add(tree);
                number++;
            }
        }
       if(number>0){
           pushService.inform("finance","有"+number+"棵苗木需要出库审核");
       }
       return Msg.newMsg(trees);
    }
    @ApiOperation(value = "更新苗木信息", notes = "传入一个或多个树的ID以;分隔,一个在id后面加上分号,树木品种treeSpecies,胸径地径chestBreadth(double型),冠幅crownWidth（double）,高度height（double），是否丛生clump, salenumber枝条数")
    @PostMapping("/updateTree")
    @Transactional
    public Msg<List<Tree>> updateTree(@RequestParam String tree_id, @RequestParam String treeSpecies,
                                      @RequestParam double chestBreadth,
                                      @RequestParam(required = false) double crownWidth,
                                      @RequestParam double height,
                                      @RequestParam Boolean clump, @RequestParam String username,
                                      @RequestParam(required = false) int salenumber,
                                      @RequestParam(value = "file", required = false) MultipartFile file
                                     ) {
        List<Tree> trees=new ArrayList<>();
        String imgUrl=FileUtil.savefile(file);
        storageService.updateinfo(tree_id,chestBreadth,-1, crownWidth,-1 ,height, -1,salenumber,-1,null, imgUrl,username,-1);
        for (String value : tree_id.split(";")) {
            Long id = Long.parseLong(value);
            Tree tree=treeDao.findOne(id);
            if(Objects.nonNull(tree)){
                trees.add(tree) ;
            }
        }
        return Msg.newMsg(trees);
    }


    @ApiOperation(value = "更新苗木信息", notes = "传入一个或多个树的ID以;分隔,一个在id后面加上分号,树木品种treeSpecies,胸径地径chestBreadth(double型),冠幅crownWidth（double）,高度height（double），是否丛生clump, salenumber枝条数")
    @PostMapping("/updateTree2")
    @Transactional
    public Msg<List<Tree>> updateTree(@RequestParam String tree_id, @RequestParam String treeSpecies,
                                      @RequestParam double chestBreadth, @RequestParam(required = false,defaultValue ="-1") double chestBreadthmax,
                                      @RequestParam(required = false) double crownWidth, @RequestParam(required = false,defaultValue ="-1") double crownWidthmax,
                                     @RequestParam double height,   @RequestParam(required = false,defaultValue ="-1") double heightmax,
                                      @RequestParam Boolean clump, @RequestParam String username,
                                      @RequestParam(required = false) int salenumber,
                                      @RequestParam(required = false) int salenumbermax,
                                      @RequestParam(value = "file", required = false) MultipartFile file,
                                      @RequestParam(required = false) double cost) {
        List<Tree> trees=new ArrayList<>();
        String imgUrl=FileUtil.savefile(file);
        storageService.updateinfo(tree_id,chestBreadth,chestBreadthmax, crownWidth,crownWidthmax ,height, heightmax,salenumber,salenumbermax,null, imgUrl,username,cost);
        for (String value : tree_id.split(";")) {
            Long id = Long.parseLong(value);
            Tree tree=treeDao.findOne(id);
            if(Objects.nonNull(tree)){
                trees.add(tree) ;
            }
        }
        return Msg.newMsg(trees);
    }

    @PostMapping("/multiadd")
    @Transactional
    public Msg<String> multiadd(@RequestParam String treeWell_ids, @RequestParam String treeSpecies, @RequestParam double chestBreadth,
                                @RequestParam double crownWidth,
                                @RequestParam double height,
                                @RequestParam Boolean clump, @RequestParam int num, @RequestParam String username,
                                @RequestParam int salenumber,
                                @RequestParam(value = "remark", required = false) String remark,@RequestParam(value = "file", required = false) MultipartFile file
                                ) {
        System.out.println("得到请求数据时间:"+System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        String imgurl= FileUtil.savefile(file);
        StringBuilder treeids=new StringBuilder();
        for (String value : treeWell_ids.split(";")) {
            Long id = Long.parseLong(value);
            Tree tree = treeWellService.saveTreeWell(id, treeSpecies, chestBreadth, -1,crownWidth, -1,height,-1,clump, num, username, salenumber,-1, serid,remark, imgurl,-1);
            treeids.append(tree.getTree_id()+";");
        }
        pushService.inform("finance","有"+num+"棵苗木需要入库审核");
        System.out.println("请求完成时间:"+System.currentTimeMillis());
        return Msg.newMsg(treeids.toString());
    }
    @PostMapping("/multiadd2")
    @Transactional
    public Msg<String> multiadd2(@RequestParam String treeWell_ids, @RequestParam String treeSpecies, @RequestParam double chestBreadth,@RequestParam(required = false) double chestBreadthmax,
                                @RequestParam double crownWidth,@RequestParam(required = false) double crownWidthmax,
                                @RequestParam double height,@RequestParam(required = false) double heightmax,
                                @RequestParam Boolean clump, @RequestParam int num, @RequestParam String username,
                                @RequestParam int salenumber,@RequestParam(required = false) int salenumbermax,
                                @RequestParam(value = "remark", required = false) String remark,@RequestParam(value = "file", required = false) MultipartFile file,
                                @RequestParam( required = false) double cost) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String serid = sdf.format(new Date());
        String imgurl= FileUtil.savefile(file);
        StringBuilder treeids=new StringBuilder();
        for (String value : treeWell_ids.split(";")) {
            Long id = Long.parseLong(value);
            Tree tree = treeWellService.saveTreeWell(id, treeSpecies, chestBreadth, chestBreadthmax,crownWidth, crownWidthmax,height,heightmax,(salenumber>0)||(salenumbermax>0), num, username, salenumber,salenumbermax, serid,remark, imgurl,cost);
            treeids.append(tree.getTree_id()+";");
        }
        pushService.inform("finance","有"+num+"棵苗木需要入库审核");
        return Msg.newMsg(treeids.toString());
    }

    @ApiOperation(value = "地块搜索", notes = "输入搜索条件如'A或搜索子地块A-1'")
    @GetMapping("/landMass")
    public Msg<List<ApiAreaModel>> landMass(@RequestParam String serialNumber) {
        List<ApiAreaModel> apiAreaModels=new ArrayList<>();
        if(serialNumber.contains("-")){
            if(serialNumber.length()>0){
                String str[]= serialNumber.split("-");
                if(Objects.nonNull(str[0])&&str[0].length()>0){
                    AreaLocation areaLocation= areaLocationDao.findbyname(str[0]);
                    if(Objects.nonNull(areaLocation)){
                        if(Objects.nonNull(str[1])&&str[1].length()>0){
                            LandMass landMass= landMassDao.findSerialNumber(areaLocation.getAreaLocation_id(),str[1]);
                            apiAreaModels.add(areaLocationService.setland(landMass));
                        }
                    }
                }
            }
        }else {
            apiAreaModels =areaLocationService.serialNumber(serialNumber);
        }
        return Msg.newMsg(apiAreaModels);
    }

    @ApiOperation(value = "树种搜索", notes = "输入树种如：“白杨树”;返回结果未3个list，第一个搜索匹配到的结果，第二个为已种植但不是搜索树种，第三个为空地块")
    @GetMapping("/treeSpecies")
    public Msg<List<Object>> treeSpecies(@RequestParam String treeSpecies) {
        List<Object> objects = new ArrayList<>();
        List<ApiAreaModel> areaLocations = areaLocationService.list(treeSpecies);
        List<ApiAreaModel> areaLocationsN = areaLocationService.notTreeSpecies(treeSpecies);
        List<ApiLandMassModel> areaLocations1 = landMassService.list(LandMass.TYPE_LANDMASS_LEISURE);
        objects.add(areaLocations);
        objects.add(areaLocationsN);
        objects.add(areaLocations1);
        return Msg.newMsg(objects);
    }

//    @ApiOperation(value = "选择种植方式", notes = "way1表示#字形，2表示品字形;gap间隙 ;地块id:landMass_id")
//    @GetMapping("/plantWay")
//    public Msg<List<Object>> treeSpecies(@RequestParam int way, @RequestParam double gap, @RequestParam Long landMass_id) {
//        List<Object> objects = new ArrayList<>();
//        LandMass landMass = landMassDao.findOne(landMass_id);
//        objects.add(landMassService.plantWay(way, gap, landMass_id));
//        objects.add(landMass);
//        return Msg.newMsg(objects);
//    }
//    @ApiOperation(value = "选择种植方式2", notes = "way1表示#字形，2表示品字形;gap间隙 ;地块id:landMass_id")
//    @GetMapping("/plantWay2")
//    public Msg<ApiAreaSummary> treeSpecies2(@RequestParam int way, @RequestParam double gap, @RequestParam Long landMass_id) {
//        landMassService.plantWay(way, gap, landMass_id);
//        Msg<ApiAreaSummary> apiAreaSummaryMsg = landmassSummary(landMass_id);
//        return apiAreaSummaryMsg;
//    }
//    @ApiOperation(value = "选择种植方式2", notes = "way1表示#字形，2表示品字形;gap间隙 ;地块id:landMass_id")
//    @GetMapping("/plantWay3")
//    public Msg<ApiAreaSummary> treeSpecies3(@RequestParam int way, @RequestParam double gap, @RequestParam Long landMass_id,@RequestParam double x) {
//        double y=x;
//        landMassService.plantWay(way, gap,gap,landMass_id,x,y);
//        Msg<ApiAreaSummary> apiAreaSummaryMsg = landmassSummary(landMass_id);
//        return apiAreaSummaryMsg;
//    }
    @ApiOperation(value = "选择种植方式", notes = "way1表示#字形,2表示品字形,3表示倒品字;xgap横向间隙;ygap纵向间距;地块id:landMass_id")
    @GetMapping("/plantWay4")
    public Msg<ApiAreaSummary> treeSpecies4(@RequestParam int way, @RequestParam double xgap,@RequestParam double ygap, @RequestParam Long landMass_id,@RequestParam double x,@RequestParam double y,@RequestParam double xright,@RequestParam double ybottom) {
        landMassService.plantWay(way, xgap,ygap,landMass_id,x,y,xright,ybottom);
        Msg<ApiAreaSummary> apiAreaSummaryMsg = landmassSummary(landMass_id);
        return apiAreaSummaryMsg;
    }
    @ApiOperation(value = "查询地块已生成树坑", notes = "地块id:landMass_id")
    @GetMapping("/getLandMass")
    public Msg<List<TreeWell>> getLandMass(@RequestParam Long landMass_id) {
        LandMass landMass = landMassDao.findOne(landMass_id);
        if (Objects.nonNull(landMass) && landMass.getTreeWells().size() > 0) {
            return Msg.newMsg(landMass.getTreeWells());
        } else {
            return Msg.fail("当前地块未生成树坑");
        }
    }
    @PostMapping(value = "/test")
    public Msg<String> test(HttpServletRequest request){
        Enumeration<String> parameterNames = request.getParameterNames();
        Map<String, String[]> parameterMap = request.getParameterMap();
        return Msg.newMsg("true");
    }

    @ApiOperation(value = "输入树种/地块", notes = "输入树种/地块如“A-1")
    @PostMapping("/landOrTreesc")
    public Msg<List<ApiLandMassSeek>> landOrTreesc(@RequestParam String condition){
        List<ApiLandMassSeek> apiLandMassSeeks=new ArrayList<>();
        if(LetterUtil.chinese(condition)){
            apiLandMassSeeks =landMassService.getTreeSpecies(condition);
        }else if(condition.contains("-")){
            apiLandMassSeeks =landMassService.getLandMass(condition);
        }else {
            apiLandMassSeeks =landMassService.getAreaLandMass(condition);
        }
        return Msg.newMsg(apiLandMassSeeks);
    }

    @ApiOperation(value = "树种/地块搜索待出库 号苗 待死苗数量", notes = "输入树种/地块如“A-1")
    @PostMapping("/searchTreeorArea")
    public Msg<List<ApiLandMassSeek>> searchTreeorArea(@RequestParam String condition){
        if(LetterUtil.chinese(condition)){
            List<ApiLandSearch> apiLandSearchList = landMassService.searchBySpecies(condition);
            return Msg.newMsg(apiLandSearchList);
        }else if(condition.contains("-")){
            List<ApiLandSearch> apiLandSearchList = landMassService.searchByLand(condition);
            return Msg.newMsg(apiLandSearchList);
        }else {
            List<ApiLandSearch> apiLandSearchList = landMassService.searchByArea(condition);
            return Msg.newMsg(apiLandSearchList);
        }
    }


    @ApiOperation(value = "查询区域id获取地块",notes = "查询区域id获取地块以及种植信息")
    @PostMapping("/individual")
    public Msg<List<ApiLandMassModel>> individual(@RequestParam Long areaLocation_id){
        List<ApiLandMassModel> apilist=new ArrayList<>();
        if(Objects.nonNull(areaLocation_id)){
            AreaLocation areaLocation=areaLocationDao.findOne(areaLocation_id);
            for (LandMass value:areaLocation.getLandMasses()) {
                ApiLandMassModel api=new ApiLandMassModel();
                api.setLandMass_id(value.getLandMass_id());
                api.setSerialNumber(value.getSerialNumber());
                int treeNumber=treeDao.findTreeNumber(value.getLandMass_id());
                List<String> lists=new ArrayList<>();
                Set<String> set=new HashSet<>();
                List<Tree> trees=treeDao.findBylandmass(value.getLandMass_id());
                for (Tree treevalue:trees) {
                    if(Objects.nonNull(treevalue.getTreeSpecies())&&treevalue.getTreeSpecies().length()>0){
                        set.add(treevalue.getTreeSpecies());
                    }
                }
                api.setTreeWell_quantity(treeNumber);
                api.setTreeWell_occupy(value.getTreeWells().size());
                lists.addAll(set);
                api.setTreesplist(lists);
                apilist.add(api);
            }
           return Msg.newMsg(apilist);
        }else {
           return Msg.fail("参数错误");
        }
    }


    @ApiOperation(value = "查询地块id获取地块种植信息",notes = "查询地块id获取地块以及种植信息")
    @PostMapping("/landplantinfo")
    public Msg<List<ApiLandMassModel>> landplantinfo(@RequestParam Long landMass_id){
        if(Objects.nonNull(landMass_id)){
            return Msg.newMsg(landMassService.landplantinfo(landMass_id));
        }else {
            return Msg.fail("参数错误");
        }
    }


    @ApiOperation(value = "根据输入条件多维度搜索",notes = "treeSpecies：树种;chestBreadth：胸径；heightmin起始高度；heightmax：截止高速 ；salenumber;是否丛生clump")
    @PostMapping("/multi")
    public Msg<List<ApiAreaModel>> multi(@RequestParam(required = false)String treeSpecies,@RequestParam(required = false)String chestBreadth,@RequestParam(required = false)String heightmin,
                                          @RequestParam(required = false)String heightmax,@RequestParam Boolean clump,@RequestParam(required = false)String salenumber) {
        System.out.println("接收到搜索请求时间:"+System.currentTimeMillis());
        List<ApiAreaModel> areaLocationsnew=landMassService.treeSearch(((Objects.nonNull(treeSpecies))?treeSpecies:""),
                ((Objects.nonNull(chestBreadth))?chestBreadth:""),((Objects.nonNull(salenumber))?salenumber:""),
                ((Objects.nonNull(heightmin))?heightmin:""),((Objects.nonNull(heightmax))?heightmax:""),(clump?1:0),null);
//        List<ApiAreaModel> areaLocationsnew=landMassService.treeSpecies(((Objects.nonNull(treeSpecies))?treeSpecies:""),
//                ((Objects.nonNull(chestBreadth))?chestBreadth:""),((Objects.nonNull(salenumber))?salenumber:""),
//                ((Objects.nonNull(heightmin))?heightmin:""),((Objects.nonNull(heightmax))?heightmax:""),(clump?1:0),null);
        System.out.println("搜索完成时间:"+System.currentTimeMillis());
        return Msg.newMsg(areaLocationsnew);
    }

    @ApiOperation(value = "根据区域id获取下一个子地块",notes = "根据区域id获取下一个子地块")
    @GetMapping("/next")
    public ApiLandmass next(@RequestParam Long areaLocation_id) {
        ApiLandmass apiLandmass= areaLocationService.next(areaLocation_id);
        return apiLandmass;
    }

    @ApiOperation(value = "保存子地块",notes = "保存子地块")
    @PostMapping("/create")
    public Msg<LandMass> create(@RequestParam Long areaLocation_id,@RequestParam String serialNumber,@RequestParam double length,@RequestParam double breath,@RequestParam double Xcoordinate,@RequestParam double Ycoordinate) {
//        ApiLandmass next = next(areaLocation_id);
        LandMass landMass=landMassService.create(areaLocation_id,serialNumber, length,breath,(long)Xcoordinate,(long)Ycoordinate);
        return Msg.newMsg(landMass);
    }
    @ApiOperation(value = "保存子地块",notes = "保存子地块")
    @PostMapping("/createts")
    public Msg<String> createts(@RequestParam Long areaLocation_id,@RequestParam String serialNumber,@RequestParam String lefttoppoint,@RequestParam String righttoppoint,@RequestParam String leftdownpoint,@RequestParam String rightdownpoint) {
        String land = landMassService.createts(areaLocation_id, serialNumber, lefttoppoint, righttoppoint, leftdownpoint, rightdownpoint);
        return Msg.newMsg("success");
    }

    @ApiOperation(value = "修改子地块",notes = "修改子地块")
    @PostMapping("/update")
    public Msg<LandMass> update(@RequestParam Long areaid,@RequestParam String serialNumber,@RequestParam double length,@RequestParam double breadth,@RequestParam Double Xcoordinate,@RequestParam Double Ycoordinate) {
        return Msg.newMsg(landMassService.updatecolumn(areaid,serialNumber, length,breadth,Xcoordinate.longValue(),Ycoordinate.longValue()));
    }

    @ApiOperation(value = "删除子地块",notes = "删除子地块")
    @PostMapping("/delland")
    public Msg<Boolean> delland(@RequestParam Long areaid,@RequestParam String serialNumber) {
        LandMass landMass = landMassDao.findSerialNumber(areaid, serialNumber);
        areaLocationService.deleteLand(landMass.getLandMass_id());
        return Msg.newMsg(true);
    }

}
